//Documentation complete
/**
 * @author Jeff
 * @version 2009AP05
 */

package control;

import broker.*;
import problemDomain.*;
import static problemDomain.ErrorBean.ErrorCode.*;

import java.util.List;

/**
 * Control object for food item operations.
 */
public class FoodItemControl extends Control {
	
	/* Attributes */
	
	private FoodItemBroker fib;
	private FoodItemTypeBroker itb;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>FoodItemControl</code>.
	 * @throws ErrorBean if the database is not available
	 */
	public FoodItemControl () throws ErrorBean {
		fib = FoodItemBroker.getBroker();
		itb = FoodItemTypeBroker.getBroker();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	/**
	 * Creates a new food item.
	 * @param name name of the food item
	 * @param description description for the food item
	 * @param cost the cost of the food item
	 * @param type the food category of the food item
	 * @param available the availability of the food item
	 * @throws ErrorBean if the input data are not correct
	 */

	public void create (String name, String description, String cost, String type, String available) throws ErrorBean {
		fib.persist(new FoodItemBean(0, name, description, checkCost(cost), type, checkAvailable(available)));
	}
	
	/**
	 * Searches for food items which match the specified search criteria.
	 * @param name the name to search for (<code>""</code> excludes this field when searching)
	 * @param type the food category to search for (<code>""</code> excludes this field when searching) 
	 * @param available the availability to search for (<code>null</code> excludes this field when searching)
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if the input data are not correct
	 */
	public List<FoodItemBean> search (String name, String type, String available) throws ErrorBean {	
		FoodItemBean searchBean = new FoodItemBean();
		searchBean.setName(name);
		searchBean.setType(type);
		searchBean.setAvailability(checkAvailable(available));
		return fib.search(searchBean);
	}
	
	/**
	 * Updates a food item.
	 * @param id the ID of the food item
	 * @param name the name of the food item
	 * @param description the description for the food item
	 * @param cost the cost of the food item
	 * @param type food category of the food item
	 * @param available the availability of the food item
	 * @throws ErrorBean if the input data are not correct
	 */
	public void update (String id, String name, String description, String cost,
			String type, String available) throws ErrorBean {
		fib.persist(new FoodItemBean(checkId(id), name, description, checkCost(cost),
			type, checkAvailable(available)));
	}
	
	/**
	 * Deletes a food item.
	 * @param id the ID of the food item
	 * @throws ErrorBean if the input data are not correct
	 */
	public void delete (String id) throws ErrorBean {
		fib.remove(new FoodItemBean(checkId(id)));
	}
	
	/**
	 * Lists all the food items.
	 * @return a <code>List</code> of the results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<FoodItemBean> listAll () throws ErrorBean {
		return fib.listAll();
	}
	
	/**
	 * Lists all the foot item types.
	 * @return a <code>List</code> of the results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<FoodItemTypeBean> listItemTypes () throws ErrorBean {
		List<FoodItemTypeBean> list = itb.listAll();
		return list;
	}
	
	/* END Methods */
	
	
	/* Data Checks */
	
	private double checkCost (String cost) throws ErrorBean {
		return checkDouble(cost, ITEM_COST_INVALID, this);}
	
	private int checkId (String id) throws ErrorBean {
		return checkInt(id, ITEM_ID_INVALID, this);}
	
	@SuppressWarnings("unused")
	private Boolean checkAvailable (String available) throws ErrorBean {
		if ("1".equals(available)) return true;
		if ("0".equals(available)) return false;
		return null;}
	
	/* END Data Checks */
	
}
